<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="离职日期" class="row-padding-bottom">
              <el-date-picker
                v-model="date"
                type="daterange"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="部门" class="row-padding-bottom">
              <el-cascader
                v-model="orgId"
                :options="orglist"
                :props="{ checkStrictly: true }"
                @change="handleChange"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="姓名" class="row-padding-bottom">
                <el-input
                  placeholder="请输入姓名"
                  @blur="filterField.name = changeValue($event)"
                  clearable
                  v-model="filterField.name"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="工资卡" class="row-padding-bottom">
                <el-select
                  v-model="filterField.hasAccount"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in hasAccountOptions"
                    :key="item.id"
                    :label="item.text"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="合同附件" class="row-padding-bottom">
                <el-select
                  v-model="filterField.hasFile"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in hasFileOptions"
                    :key="item.id"
                    :label="item.text"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
              <el-button
                class="commonBut saveBtn funComBut"
                icon="icon iconfont icondaochu"
                v-show="exportBtn"
                @click="exportExcel"
                ><span class="opetareText">导出报表</span></el-button
              >
            </el-form-item>
          </el-col>
          <!-- <el-form-item class="fr">
                            <el-button class="commonBut saveBtn" icon="icon iconfont icondaochu" v-show="exportBtn" @click="exportExcel"><span class="opetareText">导出报表</span></el-button>
                        </el-form-item> -->
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          @sort-change="sortChange"
          :data="employeeoutList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="name,idcard,orgName,employeeType"
            label="人员"
            width="230"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="isShowDetail"
                @click="toEmployeeDetail(scope.$index, scope.row)"
                >{{ scope.row.name }}</el-link
              >
              <span v-else>{{ scope.row.name || '--' }}</span>
              <p>{{ scope.row.idcard }}</p>
              <copyText
                style="color: #999; font-size: 14px"
                :tableIndex="scope.$index"
                :copyTextName="
                  scope.row.orgName +
                  (scope.row.employeeType ? '/' + scope.row.employeeType : '')
                "
                :columnName="'orgName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="录入时间"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            min-width="180px"
          ></el-table-column>
          <el-table-column
            prop="startDate,endDate"
            label="雇佣周期"
            align="center"
            min-width="180px"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.startDate }}-{{ scope.row.endDate }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="overDate"
            label="离职日期"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            min-width="120px"
          ></el-table-column>
          <el-table-column
            prop="bankName,accountId"
            label="工资卡"
            align="center"
            min-width="140px"
          >
            <template slot-scope="scope">
              <p v-if="scope.row.accountId">
                {{ scope.row.accountId | deseHandler(4, 4, '*') || '' }}
              </p>
              <p v-if="scope.row.bankName" style="color: #999; font-size: 14px">
                {{ scope.row.bankName || '' }}
              </p>
              <p v-if="!scope.row.accountId">未填写</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="hasFile"
            label="合同附件"
            align="center"
            min-width="100"
          >
            <template slot-scope="scope">
              <p v-if="scope.row.hasFile == '0'" style="color: #6dd400">
                未上传
              </p>
              <p v-else-if="scope.row.hasFile == '1'" style="color: #e02020">
                已上传
              </p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="left"
            fixed="right"
            min-width="120px"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="goEditPayCheck(scope.$index, scope.row)"
                v-show="editSalaryBtn"
                >编辑工资卡</el-link
              >
              <span v-show="!editSalaryBtn">--</span>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关离职人员数据"></emptyTable>
          </div>
        </el-table>

        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
      <el-dialog
        title="编辑工资卡"
        class="oneColumnDialog"
        width="640px"
        :visible.sync="formVisible"
        :close-on-click-modal="false"
        @close="closeDialog"
        custom-class="brand-dialog-min-width"
        :modal-append-to-body="false"
        :append-to-body="true"
      >
        <el-form
          :model="formData"
          :rules="formRules"
          ref="formData"
          label-width="150px"
        >
          <el-form-item label="人员姓名：">
            <span>{{ formData.name }}</span>
          </el-form-item>
          <el-form-item :label="'工资卡所属银行：'" prop="bankId">
            <el-select
              v-model="formData.bankId"
              placeholder="请选择"
              class="optionWidth"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) of bankData"
                :key="index"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工资卡号：" prop="accountId">
            <el-input
              v-model="formData.accountId"
              autocomplete="off"
              placeholder="请输入"
              class="inputWidth"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="cancelEdit('formData')" class="cancel"
            >取消</el-button
          >
          <el-button
            type="primary"
            @click.native="savePayCheckInfo('formData')"
            class="save"
            :loading="isSaveLoading"
            >保存</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import common from '../../kits/common'

export default {
  name: 'employeeOut',
  mixins: [scrollTable],
  data() {
    return {
      editSalaryBtn: false, //是否展示编辑工资卡按钮
      exportBtn: false, //导出报表
      formLabelWidth: '125px',
      titleText: '离职人员',
      advanced: false,
      titleExplain: '记录公司人员离职记录，在公司有多次离职记录，展示为多条',
      orglist: [], //部门
      // 工资卡下拉框
      hasAccountOptions: [
        { id: '1', text: '已填写' },
        { id: '0', text: '未填写' },
      ],
      //下拉框数据
      hasFileOptions: [
        { id: '1', text: '已上传' },
        { id: '0', text: '未上传' },
      ],
      //表格数据
      employeeoutList: [],
      requestUrl: 'api-e/nc/employee/expand/dismissed',
      date: [], //日期区间
      orgId: '',
      //搜索条件
      filterField: {
        orgId: '', //部门
        overDateStart: '',
        overDateEnd: '',
        name: '', //姓名
        hasAccount: '', //有无工资卡
        hasFile: '', //有无合同附件
      },
      pageSize: 20,
      pageNo: 1,
      employeeId: '', //务工id
      contractId: '', //合同id
      //是否显示弹窗
      showDrawer: false,
      value: [],
      editPayCheck: false, //编辑工资卡
      formData: '', //编辑工资卡获取的行的数据
      bankData: [], //银行下拉框
      formVisible: false, //编辑工资卡弹框
      formRules: {
        bankId: [
          {
            required: true,
            message: '工资卡所属银行不能为空',
            trigger: 'change',
          },
        ],
        accountId: [
          {
            required: true,
            pattern: /^\d{15,19}$/,
            message: '请输入正确的工资卡号',
            trigger: 'blur',
          },
        ],
      },
      isShow: false, //列表加载
      loading: true,
      isSaveLoading: false, // 保存loading
      isShowDetail: '',
    }
  },
  created() {
    // 部门下拉框
    this.getOrglist()
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    //弹框银行下拉框
    this.getBankData()

    //是否展示编辑工资卡按钮
    this.editSalaryBtn =
      this.$store.state.buttonLimitsArr.indexOf('ncEmployeeOut:update') > -1
    this.exportBtn =
      this.$store.state.buttonLimitsArr.indexOf('ncEmployeeOut:export') > -1
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('ncEmployeeOut:detail') > -1
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getOrglist() {
      this.axios({
        method: 'post',
        url: 'api-c/organization/base/org/arc',
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.orglist = response.data.result
            } else {
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    handleChange(item) {
      var orgId = this.orgId[this.orgId.length - 1]
      this.filterField.orgId = orgId
    },
    getTableList(data) {
      this.employeeoutList = data
      this.loading = false
      if (this.employeeoutList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      if (this.date) {
        this.filterField.overDateStart = this.date[0]
        this.filterField.overDateEnd = this.date[1]
      } else {
        this.filterField.overDateStart = ''
        this.filterField.overDateEnd = ''
      }
      this.$refs.pagination.getTableListData()
    },
    // 导出
    exportExcel() {
      if (this.employeeoutList.length > 0) {
        if (this.filterField.overDateStart == undefined) {
          this.filterField.overDateStart = ''
          this.filterField.overDateEnd = ''
        }
        window.location.href =
          common.commonApi +
          'api-e/nc/employee/expand/export/dismissed?orgId=' +
          this.filterField.orgId +
          '&name=' +
          this.filterField.name +
          '&hasAccount=' +
          this.filterField.hasAccount +
          '&hasFile=' +
          this.filterField.hasFile +
          '&overDateStart=' +
          this.filterField.overDateStart +
          '&overDateEnd=' +
          this.filterField.overDateEnd +
          '&sortField=' +
          this.filterField.sortField +
          '&sort=' +
          this.filterField.sort +
          '&access_token=' +
          localStorage.getItem('access_token')
      } else {
        this.$message({
          type: 'error',
          message: '无数据导出',
        })
      }
    },
    //显示人员详情传参
    toEmployeeDetail(index, row) {
      this.employeeId = row.id
      this.contractId = row.contractId
      this.$router.push({
        path: '/home/employee/employeeDetail',
        name: 'employeeDetail',
        query: {
          employeeId: this.employeeId,
          contractId: this.contractId,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/employee/employeeOut',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    // // 编辑工资卡
    goEditPayCheck(index, row) {
      this.formVisible = true
      this.formData = Object.assign({}, row)
    },
    getBankData() {
      var data = {
        type: 'BANK_TYPE',
      }
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/findbytype',
        data: data,
      })
        .then((response) => {
          this.bankData = response.data.result
        })
        .catch((error) => {})
    },
    //关闭弹窗
    closeDialog() {
      this.$refs['formData'].resetFields()
    },
    // 弹框取消
    cancelEdit() {
      this.formVisible = false
      this.$refs['formData'].resetFields()
    },
    savePayCheckInfo() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.editPayCardInfo()
        } else {
          return false
        }
      })
    },
    editPayCardInfo() {
      this.isSaveLoading = true
      this.axios({
        method: 'post',
        url: 'api-e/nc/contract/expand/setaccount',
        data: {
          id: this.formData.contractId,
          accountId: this.formData.accountId,
          bankId: this.formData.bankId,
        },
      })
        .then((response) => {
          this.isSaveLoading = false
          if (!response.data.hasError) {
            this.$message({
              type: 'success',
              message: response.data.msg,
            })
            this.$refs['formData'].resetFields()
            this.formVisible = false
            this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
            this.isSaveLoading = false
          }
        })
        .catch((error) => {
          this.$message.error('保存失败')
          this.isSaveLoading = false
        })
    },
  },
}
</script>

<style lang="less" scoped>
.mainCon {
  top: 212px;
}
@import '../../../static/css/filterArea.less';
</style>
